package com.fzy.core.system.action;

import ge.core.system.entity.Entity;
import ge.core.system.entity.Mapping;
import ge.core.system.service.MappingListVo;

import java.util.Date;
import java.util.List;

import com.fzy.core.entity.Page;
import com.fzy.core.system.service.DbService;
import com.fzy.core.system.service.EntityQuery;
import com.fzy.core.utils.U;
import com.fzy.core.webwork.BaseAction;
import com.fzy.core.webwork.MyValidationAware;
import com.opensymphony.xwork.ModelDriven;
import com.opensymphony.xwork.Preparable;

public class MappingInputAction extends BaseAction implements
		MyValidationAware, Preparable, ModelDriven {

	private static final long serialVersionUID = -6545776513894162109L;
	String errorDispatch;
	Mapping mapping;
	DbService dbService;
	boolean isEntityFound;
	Page tables;
	Page entities;
	List mappingList;
	EntityQuery tableQuery;
	EntityQuery entityQuery;
	Entity defaultEntity;

	public void prepare() throws Exception {
		errorDispatch = "dispatcher:/pages/refresh-parent.jsp";
		mapping = (Mapping) validateIdAndGetEntity(request.getParameter("id"),
				Mapping.class);

		// only for delete operation
		isEntityFound = mapping != null;

		// default values
		if (mapping == null) {
			mapping = new Mapping();
			mapping.setNameText("default");
			mapping.setPackageText("ge");
			mapping.setPrefixText("");
		}
		tables = new Page();
		entities = new Page();
		tableQuery = new EntityQuery();
		entityQuery = new EntityQuery();
		
		defaultEntity = new Entity();
		defaultEntity.setPackageText("ge.app");
		defaultEntity.setHasPrefix("0");
		defaultEntity.setIsWork("1");

	}

	public String execute() throws Exception {
		if(mapping.getId() != null){
			entities = dbService.listEntity(mapping.getId(), entityQuery, entities);
			tables = dbService.getTables(mapping, tableQuery, tables);
		}
		mappingList = dbService.getMappings();
		MappingListVo vo = new MappingListVo();
		vo.setId(new Long(0));
		vo.setNameText("请选择");
		mappingList.add(0, vo);
		return "dispatcher:/WEB-INF/jsp/core/system/mapping-input.jsp";
	}

	public String delete() throws Exception {
		if (isEntityFound) {
			mapping.setStatus("0");
			mapping.setUpdatedDate(new Date());
			mapping.setUpdatedUser(U.getUser());
			dbService.update(mapping);
			
			addActionMessage(getText("success.save"));
		} else {
			addActionError(getText("error.entityMissing",
					new String[] { request.getParameter("id") }));
		}

		return "dispatcher:/pages/refresh-parent.jsp";
	}

	public String getErrorDispatch() {
		return errorDispatch;
	}

	public void setDbService(DbService dbService) {
		this.dbService = dbService;
	}

	public Object getModel() {
		return mapping;
	}

	public Page getEntities() {
		return entities;
	}

	public Page getTables() {
		return tables;
	}

	public List getMappingList() {
		return mappingList;
	}

	public EntityQuery getEntityQuery() {
		return entityQuery;
	}

	public EntityQuery getTableQuery() {
		return tableQuery;
	}

	public Entity getDefaultEntity() {
		return defaultEntity;
	}

}
